package anna.lopez.mora;

import java.util.ArrayList;
import java.util.Random;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class Nuvols {
	BitmapFactory.Options options;
	
	Bitmap nuvol1, nuvol2;
	ArrayList<Objecte> nuvols;
	private int maxmario;
	private Random rand;
	
	public Nuvols() {
		rand = new Random();
//		maxmario = rand.nextInt(5);
		maxmario = 5;
		nuvols = new ArrayList<Objecte>();
		options = new BitmapFactory.Options();
		options.inPreferredConfig = Bitmap.Config.ARGB_8888;
	}
	
	public void actualitza() {
		crea_mario();
		for(int i = 0; i < nuvols.size();++i){
			Objecte o = nuvols.get(i);
			o.actualitza();
			if((o.vx > 0 && o.x > GV.Screen.metrics.widthPixels) || (o.vx < 0 && o.x < -o.tx)) nuvols.remove(i);
		}
	}
	
	public void crea_mario() {
		if(nuvols.size() == maxmario) return;
			for(int i = nuvols.size(); i < maxmario; ++i){
				Bitmap mario;
				if(rand.nextInt(2)==0) mario = BitmapFactory.decodeResource(GV.Instancies.background.getResources(), R.drawable.cloud1, options);
				else mario = BitmapFactory.decodeResource(GV.Instancies.background.getResources(), R.drawable.cloud2, options);
				float x,y,tx,ty,vx,vy;
				tx = (90f+(float)rand.nextInt(180))/1000*GV.Screen.metrics.widthPixels;
				ty = (100.5f +(float)rand.nextInt(201))/1000*GV.Screen.metrics.heightPixels;
				y = (float)rand.nextInt(GV.Screen.metrics.heightPixels-(int)ty);
				vx = (0.0050f+(float)rand.nextInt(40)/10000)*GV.Screen.metrics.widthPixels;
				vy = 0;
				if(rand.nextInt(2)==0) {
					x = -tx;
				}
				else {
					x = GV.Screen.metrics.widthPixels;
					vx = -vx;
				}
				nuvols.add(new Objecte(mario,x,y,tx,ty,vx,vy));
			}
	}
	
	public void draw(Canvas canvas) {
		for(int i=0; i<nuvols.size(); i++) {
			Objecte o = nuvols.get(i);
			o.draw(canvas);
		}
	}
}
